using UnityEngine;

public class ThirdPersonPlayer : MonoBehaviour
{
    [Header("移动设置")]
    public float walkSpeed = 6f;
    public float runSpeed = 12f;
    public float jumpHeight = 3f;
    public float gravity = -9.81f;
    
    [Header("旋转设置")]
    public float rotationSpeed = 10f;
    
    [Header("组件引用")]
    public CharacterController controller;
    public Transform cameraTransform;
    
    private Vector3 velocity;
    private bool isGrounded;
    private float currentSpeed;
    
    void Start()
    {
        // 如果没有设置CharacterController，尝试获取
        if (controller == null)
            controller = GetComponent<CharacterController>();
            
        // 如果没有设置摄像机，尝试找到主摄像机
        if (cameraTransform == null)
        {
            Camera mainCam = Camera.main;
            if (mainCam != null)
                cameraTransform = mainCam.transform;
        }
    }
    
    void Update()
    {
        HandleGravity();
        HandleMovement();
        HandleJump();
    }
    
    void HandleGravity()
    {
        // 检查是否在地面上
        isGrounded = controller.isGrounded;
        
        if (isGrounded && velocity.y < 0)
        {
            velocity.y = -2f; // 重置垂直速度
        }
        
        // 应用重力
        velocity.y += gravity * Time.deltaTime;
        controller.Move(velocity * Time.deltaTime);
    }
    
    void HandleMovement()
    {
        // 获取输入
        float horizontal = Input.GetAxis("Horizontal");
        float vertical = Input.GetAxis("Vertical");
        
        // 计算移动方向（相对于摄像机）
        Vector3 moveDirection = Vector3.zero;
        
        if (cameraTransform != null)
        {
            // 获取摄像机的右方向和前方向（忽略Y轴）
            Vector3 cameraRight = cameraTransform.right;
            Vector3 cameraForward = cameraTransform.forward;
            
            cameraRight.y = 0;
            cameraForward.y = 0;
            
            // 标准化方向向量
            cameraRight.Normalize();
            cameraForward.Normalize();
            
            // 计算移动方向
            moveDirection = cameraRight * horizontal + cameraForward * vertical;
        }
        else
        {
            // 如果没有摄像机，使用世界坐标
            moveDirection = transform.right * horizontal + transform.forward * vertical;
        }
        
        // 确定移动速度
        bool isRunning = Input.GetKey(KeyCode.LeftShift);
        currentSpeed = isRunning ? runSpeed : walkSpeed;
        
        // 移动角色
        if (moveDirection.magnitude > 0.1f)
        {
            controller.Move(moveDirection * currentSpeed * Time.deltaTime);
            
            // 让角色朝向移动方向
            Quaternion targetRotation = Quaternion.LookRotation(moveDirection);
            transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationSpeed * Time.deltaTime);
        }
    }
    
    void HandleJump()
    {
        if (Input.GetButtonDown("Jump") && isGrounded)
        {
            velocity.y = Mathf.Sqrt(jumpHeight * -2f * gravity);
        }
    }
    
    // 获取当前移动速度（用于动画或其他系统）
    public float GetCurrentSpeed()
    {
        return controller.velocity.magnitude;
    }
    
    // 获取是否在地面上
    public bool IsGrounded()
    {
        return isGrounded;
    }
    
    // 获取是否在跑步
    public bool IsRunning()
    {
        return Input.GetKey(KeyCode.LeftShift) && GetCurrentSpeed() > walkSpeed * 0.5f;
    }
}
